%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void yyerror (char const *);
%}

/*
  start production (non-terminal)
*/
%start input

%union {
  float val;
}

%token NEWLINE
%token PLUS
%token MINUS
%token DIVIDE
%token TIMES
%token <val> NUM

%type <val> expr

%%

input:
     /* empty */
     | line input 
     ;

line : NEWLINE
     | expr NEWLINE {printf("%.2f\n", $1);}
     | error NEWLINE;

expr : NUM              { $$ = $1;       }
     | PLUS expr expr   { $$ = $2 + $3;  }
     | MINUS expr expr  { $$ = $2 - $3;  }
     | DIVIDE expr expr { $$ = $2 / $3;  }
     | TIMES expr expr  { $$ = $2 * $3;  }
     ;

%%

int main(int argc, char* argv[]){
    return yyparse();
}

void yyerror(char const *s){
    fprintf(stderr, "syntax error\n");
}

